﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Support.DTO;
using System.Data.SqlClient;
using System.Data;

namespace DataAccess
{
    public class TipoPlanDAL : BaseSQLDAL
    {
        public List<TipoPlanDTO> GetTiposPlan()
        {
            List<TipoPlanDTO> listaTipos = new List<TipoPlanDTO>();

            try
            {

                CreateCommand("TipoPlan_Listado", true);

                ExecuteReader();
                while (Read())
                {
                    listaTipos.Add(FillDTO(GetDataReader()));
                }
                return listaTipos;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DisposeCommand();
                DisposeDataReader();
                CloseConnection();
            }
        }

        public List<TipoPlanDTO> PlanBuscarXNombre(string nombre)
        {
            List<TipoPlanDTO> listaTipos = new List<TipoPlanDTO>();

            try
            {
                CreateCommand("TipoPlan_BuscarXNombre", true);
                AddCmdParameter("@Nombre", string.IsNullOrEmpty(nombre) ? "" : nombre, ParameterDirection.Input);
                ExecuteReader();
                while (Read())
                {
                    listaTipos.Add(FillDTO(GetDataReader()));
                }
                return listaTipos;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DisposeCommand();
                DisposeDataReader();
                CloseConnection();
            }
        }




        private TipoPlanDTO FillDTO(SqlDataReader reader)
        {

            return new TipoPlanDTO()
            {
                Id = Convert.ToInt32(GetOutResult("IdTipoPlan")),
                Nombre = Convert.ToString(GetOutResult("Nombre")),
                Descripcion = Convert.ToString(GetOutResult("Descripcion")),
                PrecioTPlan = Convert.ToString(GetOutResult("PrecioTipoPlan")),

            };

        }

        public int InsertarTipo(TipoPlanDTO planparam)
        {

            try
            {
                CreateCommand("TipoPlan_Insertar", true);
                // AddCmdParameter("@Codingresado", nomenparam.IdIdNomenclador, ParameterDirection.Input);
                AddCmdParameter("@Nombre", planparam.Nombre, ParameterDirection.Input);
                AddCmdParameter("@Descripcion", planparam.Descripcion, ParameterDirection.Input);
                AddCmdParameter("@Precio", planparam.PrecioTPlan, ParameterDirection.Input);
                ExecuteReader();
                Read();
                int result = Convert.ToInt32(GetOutResult("plan_id"));
                return result;

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DisposeCommand();
                DisposeDataReader();
                CloseConnection();
            }
        }

        public void UpdateTipoPlan(TipoPlanDTO planparam)
        {

            try
            {

                CreateCommand("TipoPlan_Modificar", true);
                // AddCmdParameter("@Codingresado", nomenparam.IdIdNomenclador, ParameterDirection.Input);
                AddCmdParameter("@Nombre", planparam.Nombre, ParameterDirection.Input);
                AddCmdParameter("@Descripcion", planparam.Descripcion, ParameterDirection.Input);
                AddCmdParameter("@Precio", planparam.PrecioTPlan, ParameterDirection.Input);
                AddCmdParameter("@Idplan", planparam.Id, ParameterDirection.Input);
                ExecuteReader();
                Read();
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DisposeCommand();
                DisposeDataReader();
                CloseConnection();
            }
        }

        public bool EliminarPlan(int planId)
        {
            try
            {
                CreateCommand("TipoPlan_Eliminar", true);
                AddCmdParameter("@TipoPlanId", planId, ParameterDirection.Input);

                ExecuteReader();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                DisposeCommand();
                CloseConnection();
            }
        }

    }
}
